package com.swing.trial;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import net.miginfocom.swing.MigLayout;

import com.workoutdiary.dao.MCycleRecordDao;
import com.workoutdiary.dao.UserDao;
import com.workoutdiary.entities.MCycleRecord;
import com.workoutdiary.entities.User;

public class MainAreaFrame implements TableListener {

	private JFrame frame;
	private JTable tableRecords;
	private JMenuItem mntmLogin;
	private JMenuItem mntmMcyclerecord;
	private JScrollPane scrollPane;
	
	private FormListener formListener;
	
	
	public void setFormListener(FormListener listener) {
		this.formListener = listener;
	}

	public void show() {
		frame.setVisible(true);
	}
	
	/**
	 * Create the application.
	 */
	public MainAreaFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new MigLayout("", "[81.00][393.00,grow]", "[47.00][132.00,grow]"));
		
		scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, "cell 1 1,grow");
		
//		tableRecords = new JTable();
//		scrollPane.setViewportView(tableRecords);
		frame.setTitle("Workout Diary");
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu Main = new JMenu("Main");
		menuBar.add(Main);
		
		mntmLogin = new JMenuItem("Login");
		Main.add(mntmLogin);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		Main.add(mntmExit);
		
		JMenu Record = new JMenu("Record");
		menuBar.add(Record);
		
		mntmMcyclerecord = new JMenuItem("MCycleRecord");
		Record.add(mntmMcyclerecord);
		setActions();
		fillTableRecords(null, null);
	}
	
	private void setActions() {
		final TableListener tableListener = this;
		mntmLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				LoginFrame loginFrame = new LoginFrame();
				loginFrame.setFormListener(formListener);
				loginFrame.setTableListener(tableListener);
				loginFrame.show();
			}
		});
		
		mntmMcyclerecord.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AddMCycleRecordFrame addRecord = new AddMCycleRecordFrame();
				addRecord.setTableListener(tableListener);
				addRecord.setVisible(true);
			}
		});
	}
	
	@Override
	public void fillTableRecords(Object[][] rowData, Object[] columnNames) {
		String[] headers = new String[]{"1", "2", "3", "4", "5"};
		Object[][] data = new Object[][]{{"a","a"},{"b", "b"}};
		tableRecords = new JTable(data, headers);
//		UserDao userDao = new UserDao(); 
//		data = userDao.getUsersMatrix(userDao.getAll());
		
		MCycleRecordDao dao = new MCycleRecordDao();
		data = dao.getRecordsMatrix(dao.getAll());
		
		System.out.println(dao.getAll().size());
		System.out.println(data.length);
		
		headers = new String[]{"id", "date", "fase", "description"};
		
		DiaryTableModel model = new DiaryTableModel(data, headers);
		
		tableRecords.setModel(model);
		scrollPane.setViewportView(tableRecords);
		
	}
	
	public void refreshTable() {
		MCycleRecordDao dao = new MCycleRecordDao();
		List<MCycleRecord> recordsList = dao.getAll();
		Object[][] data = dao.getRecordsMatrix(recordsList);
		
		String[] headers = new String[]{"id", "date", "fase", "description"};
		
		DiaryTableModel model = new DiaryTableModel(data, headers);
		
		tableRecords.setModel(model);
		scrollPane.setViewportView(tableRecords);
	}
}
